<?php

namespace app\admin\controller;
use app\common\service\BalanceService;
use think\Db;

//转账管理
class Transfer extends Common{


	public function index(){
		return $this->fetch();
	}


	public function dataList(){
		$limit = request()->param('limit')?:20;
		$username  = request()->param('username');
		$to_name   = request()->param('to_name');
		$phone       = request()->param('phone');
		$to_phone  = request()->param('to_phone');
		$coin_name = request()->param('coin_name');
		$start_time = request()->param('start_time');
		$end_time   = request()->param('end_time');
		$where     = '1=1';
		if ($username != '') {
            $where .= ' and u1.username like \'%' . $username . '%\'';
        }
        if ($coin_name != '') {
            $where .= ' and w.name like \'%' . $coin_name . '%\'';
        }
        if ($to_name != '') {
            $where .= ' and u2.username like \'%' . $to_name . '%\'';
        }
        if($phone != '') {
            $where .= ' and u1.phone='.$phone;
        }
        if($to_phone != '') {
            $where .= ' and u2.phone='.$to_phone;
        }
        if($start_time != '') {
            $where .= ' and t.date>="'.$start_time.'"';
        }
		if($end_time != '') {
            $where .= ' and t.date<="'.$end_time.'"';
        }
		$data  = Db::table('tp_transfer_record')
				->alias('t')
				->join('tp_user u1','u1.id = t.uid','LEFT')
				->join('tp_user u2','u2.id = t.to_uid','LEFT')
				->join('tp_coin_wallet w','w.id = t.wid','LEFT')
				->whereTime('t.date','>=','2021-05-16 00:00:00')
				->where($where)
				->order('id desc')
				->field('t.*,u1.username,u1.phone,u2.username to_name,u2.phone to_phone,w.name wallet_name')
				->paginate($limit);
		$data = $data->toArray();
		if(empty($data['data'])){
			$data['data']  = Db::table('tp_transfer_record')
				->alias('t')
				->join('tp_user u1','u1.id = t.uid','LEFT')
				->join('tp_user u2','u2.id = t.to_uid','LEFT')
				->join('tp_coin_wallet w','w.id = t.wid','LEFT')
				->whereTime('t.date','>=','2021-05-16 00:00:00')
				->where($where)
				->order('id desc')
				->field('t.*,u1.username,u1.phone,u2.username to_name,u2.phone to_phone,w.name wallet_name')
				->page(1)
				->limit($limit)
				->select();		
		}
		return $this->tableData($data);
	}

	public function excel(){
		$username  = request()->param('username');
		$to_name   = request()->param('to_name');
		$phone       = request()->param('phone');
		$to_phone  = request()->param('to_phone');
		$coin_name = request()->param('coin_name');
		$start_time = request()->param('start_time');
		$end_time   = request()->param('end_time');
		$where     = '1=1';
		if ($username != '') {
            $where .= ' and u1.username like \'%' . $username . '%\'';
        }
        if ($coin_name != '') {
            $where .= ' and w.name like \'%' . $coin_name . '%\'';
        }
        if ($to_name != '') {
            $where .= ' and u2.username like \'%' . $to_name . '%\'';
        }
        if($phone != '') {
            $where .= ' and u1.phone='.$phone;
        }
        if($to_phone != '') {
            $where .= ' and u2.phone='.$to_phone;
        }
        if($start_time != '') {
            $where .= ' and t.date>="'.$start_time.'"';
        }
		if($end_time != '') {
            $where .= ' and t.date<="'.$end_time.'"';
        }
		$data  = Db::table('tp_transfer_record')
				->alias('t')
				->join('tp_user u1','u1.id = t.uid','LEFT')
				->join('tp_user u2','u2.id = t.to_uid','LEFT')
				->join('tp_coin_wallet w','w.id = t.wid','LEFT')
				->whereTime('t.date','>=','2021-05-16 00:00:00')
				->where($where)
				->order('t.id desc')
				->field('u1.phone,u1.username,u2.phone to_phone,u2.username to_name,w.name wallet_name,t.amount,t.date')
				->select();
		return exportExcel(['转账方电话','转账方','收款方电话','收款方','转账币种','转账数量','转账时间'], $data);
	}
}